define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
    require("cordova!com.justep.cordova.plugin.weixin.v3");
	var Model = function(){
	    this.timer = 0;
	    this.pointData = null;
	    this.fileList = [];
	    this.imgSelectIndex = -1;//当前选择的图片index
		this.callParent();
	};

    //打开支付选择对话框
	Model.prototype.paySelectClick = function(event){
	
	     
	     var currentRow = this.comp("payData").getFirstRow();
	     console.log(currentRow);
	     if(!currentRow.val("point.id")){
	    	 justep.Util.hint("未选择点位", {type:'danger'});
	     }else if(!currentRow.val("adTime")){
	    	 justep.Util.hint("未选择时间", {type:'danger'});
	     }else if(!currentRow.val("adCycle")){
	    	 justep.Util.hint("未选择周期", {type:'danger'});
	     }else if(this.fileList.length === 0){
	    	 justep.Util.hint("未添加图片", {type:'danger'});
	     }else{
	         if(this.comp("payOptionData").getCurrentRowID()==1){
	            this.comp("popOver1").show();
				this.payOrderByWeixin();
				
			 }    	 
	     } 
	};

	Model.prototype.modelLoad = function(event){
	     var me = this;
	     var url = "http://www.xjzlhy.com:81/ex_community/a/api/community/getAdPoint?callback=?";
	     $.getJSON(url,
	    		 function(data){
	    		   
	    		    me.pointData = data.data[0];
	    		 	me.comp("pointData").loadData(data.data[0]);
	    		 	me.comp("timeData").loadData(data.data[1]);
	    		 	me.comp("cycleData").loadData(data.data[2]);
	    		 }
	     );
	     
	     if(this.fileList.length !== 0){
	    	 $("#"+this.getIDByXID("deleteBtn")).show();
	     }
	     
	};

    //选择要添加的图片
	Model.prototype.file2Change = function(event){
	   $(".preview img").off("click");
	   var me = this;
	   me.imgSelectIndex = me.fileList.length-1;
		this.fileList.push(event.currentTarget.files[0]);
		var src = window.URL.createObjectURL(event.currentTarget.files[0]);
		$("#"+this.getIDByXID("imgPreview")).append("<img src='"+src+"' style='margin-right:5px;' class='img-thumbnail imgUnSelect' />");
		$(".preview img").each(function(index){
			$(this).on("click",function(event){
				 me.imgSelectIndex = index;
				 $(this).toggleClass("imgUnSelect");
				 $(this).toggleClass("imgSelect");
				 $(this).siblings().removeClass("imgSelect");
				 $(this).siblings().addClass("imgUnSelect");
				 if($(this).hasClass("imgUnSelect")){
					 me.imgSelectIndex = -1;
				 }
			});
		});
		if(this.fileList.length == 1){
			$("#"+this.getIDByXID("deleteBtn")).show();
		}
	};

   
	//删除照片按钮

	Model.prototype.deleteBtnClick = function(event){
	    
	    	if(this.imgSelectIndex == -1){
	    		justep.Util.hint("请选择要删除的图片", {type:"danger"});    	
	    	}else{
	    		this.fileList.splice(this.imgSelectIndex,1);
		    	$(".preview img").eq(this.imgSelectIndex).remove();
		    	this.imgSelectIndex = this.fileList.length - 1;
		        if(this.fileList.length === 0){
		        	$("#"+this.getIDByXID("deleteBtn")).hide();
		        }
	    	}
	};
	
	
	Model.prototype.payOrderByWeixin = function(payDtd, orderID) {
	    var me = this;
		if (!navigator.weixin) {
			//payDtd.reject(-13);
			return;
		}
		var notifyUrl = location.origin;
		var traceID = justep.UUID.createUUID();
		var traceNo = justep.UUID.createUUID();

		var weixin = navigator.weixin;
		weixin.generatePrepayId({
			"body" : "广告费",
			"feeType" : "1",
			"notifyUrl" : notifyUrl,
			"totalFee" : "1",
			"traceId" : traceID,
			"tradeNo" : traceNo
		}, function(prepayId) {
			weixin.sendPayReq(prepayId, function(message) {
				var responseCode = parseInt(message);
				if (responseCode === 0) {
					 me.weiXinPaySuccess();
				} else if (!isNaN(responseCode)) {
				     me.comp("popOver1").hide();
				} else {	
					 me.comp("popOver1").hide();
				}
			}, function(message) {
				justep.Util.hint("微信支付失败！");
			});
		}, function(message) {
		    
			justep.Util.hint("微信支付失败！");
		});
	};

    Model.prototype.weiXinPaySuccess = function(){ 
    	     var me = this;
		     var currentRow = this.comp("payData").getFirstRow();
			 var formData = new FormData();	 
			 
			 formData.append("point.id",currentRow.val("point.id"));
			 console.log('point.id',currentRow.val("point.id"));
			 formData.append("user.id",this.getParentModel().getUserId());
			 console.log('userid',this.getParentModel().getUserId());
			 formData.append("adTime",currentRow.val("adTime"));
			 console.log('adtime',currentRow.val("adTime"));
			 formData.append("adCycle",currentRow.val("adCycle"));
			 console.log('adcyclee',currentRow.val("adCycle"));
			 formData.append("rmbCount",currentRow.val("rmbCount"));
			 for(var i = 0;i < me.fileList.length;i++){
				 formData.append("image"+i,this.fileList[i]);
			 }
			 
			 console.log('filelist',me.fileList);
			 
			 $.ajax({
				  url: "http://www.xjzlhy.com:81/ex_community/a/api/community/adRecord",
				  type: "POST",
				  data: formData,
				  processData: false,  // 告诉jQuery不要去处理发送的数据
				  contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
				  success:function(result){
				      me.comp("popOver1").hide();
					  //justep.Shell.closePage();
				  }				  
				}); 
    };
    
    
	
	
	
	//打开选则点位窗口
	Model.prototype.selectPointBtnClick = function(event){
	
		this.comp("selectPointDlg").open({data:this.pointData});

	};
	
	Model.prototype.selectPointDlgReceive = function(event){
		var selectPointName = '';
		var selectPointId = '';
		if(event.data){
			for(var i = 0;i<event.data.length;i++){
			    
				selectPointName += event.data[i].val('name');
				
				
				selectPointId += event.data[i].val('id');
				
				if(i != event.data.length-1){
					selectPointName += ',';
					selectPointId += ',';
				}
				
			}
			
			this.comp("input1").val(selectPointName);
			
			this.comp("payData").getFirstRow().val('point.id',selectPointId);
		}

	};
	return Model;
});